﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.User;
using CE.Global;
public partial class User_ResetPassword : MyLanguageBasePage
{
    protected override void InitializeCulture()
    {
        Session["Language"] = "th-TH";
        Culture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null)
        {
            //Response.Redirect("~/");
        }

        if (!Page.IsPostBack)
        {
            Session["UMMSG"] = null;
            if (Request.Params["authkey"] != null && Request.Params["uid"] != null)
            {
                if (Request.Params["authkey"].Length > 0 && Request.Params["uid"].Length > 0)
                {
                    //lblAutoKey.Text = Request.Params["authkey"].ToString();
                    //lblGUID.Text = Request.Params["uid"].ToString();
                    ViewState["AutoKey"] = Request.Params["authkey"].ToString();
                    ViewState["UID"] = Request.Params["UID"].ToString();
                    Account _account = new Account();
                    AccountDB _accountdb = new AccountDB();
                    _account = _accountdb.GetUserAccountByAuthKey(Request.Params["authkey"].ToString(), Request.Params["uid"].ToString());
                    if (_account != null)
                    {
                        txtNewPassword.Enabled = true;
                        txtRetypeNewPassword.Enabled = true;
                        btnSubmit.Enabled = true;
                    }
                    else
                    {
                        lblMessage.Text = "Invalid User ID.";
                        txtNewPassword.Enabled = false;
                        txtRetypeNewPassword.Enabled = false;
                        btnSubmit.Enabled = false;
                    }
                }
                else
                {
                    lblMessage.Text = "Invalid User ID.";
                    txtNewPassword.Enabled = false;
                    txtRetypeNewPassword.Enabled = false;
                    btnSubmit.Enabled = false;
                }
            }
            else
            {
                lblMessage.Text = "Please try again.";
                txtNewPassword.Enabled = false;
                txtRetypeNewPassword.Enabled = false;
                btnSubmit.Enabled = false;
                //Response.Redirect("~/");
            }
        }
    }

    /*
    protected void CheckPasswordLength(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the lenght of value is < 7 or Is Number Value.
            string valArgs = args.Value;
            if (valArgs.IndexOf(' ') != -1 || valArgs.IndexOf('\'') != -1 ||
                valArgs.IndexOf('\"') != -1 || valArgs.IndexOf('~') != -1)
            {
                ((CustomValidator)source).ErrorMessage = "Password include invalid character or space!";
                ((CustomValidator)source).Text = "Password include invalid character or space!";
                args.IsValid = false;
            }
            else if (args.Value.Trim().Length < 6)
            {
                ((CustomValidator)source).ErrorMessage = "Password length should be at 6!";
                ((CustomValidator)source).Text = "Password length should be at 6!";
                args.IsValid = false;
            }
            else if (args.Value.Trim().Length > 20)
            {
                ((CustomValidator)source).ErrorMessage = "Password length should not be over 20!";
                ((CustomValidator)source).Text = "Password length should not be over 20!";
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }
    */
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ViewState["AutoKey"] != null && ViewState["UID"] != null)
        {
            //Account _account = new Account();
            AccountDB _accountdb = new AccountDB();
            if (_accountdb.ResetPassword(txtNewPassword.Text, ViewState["AutoKey"].ToString(), ViewState["UID"].ToString()))
            {
                //Response.Redirect("~/user/usermessage.aspx?ErrorCode=1992");
                //Response.Redirect("~/user/usermessage.aspx?mt=UM&ec=1992");
                string _redirectUrl = CE.Global.General.GetUrl("~/user/usermessage.aspx", Page);
                Session["UMMSG"] = "UM,1992";
                Response.Redirect(_redirectUrl);
            }
            else
            {
                lblMessage.Text = "Reset password failed!.";
            }
        }
        else
        {
            lblMessage.Text = "Invalid key.";
        }
    }
}
